<form id="addCatalogForm" lay-filter="addCatalogForm" class="layui-form model-form ">
    <div class="layui-form-item">
        <label class="layui-form-label">目录名称</label>
        <div class="layui-input-block">
            <input name="name" placeholder="请输入目录名称" type="text" class="layui-input" maxlength="20"
                   lay-verType="tips" lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上级名称</label>
        <div class="layui-input-block">
            <input name="parent_name" placeholder="请输入上级名称" type="text" class="layui-input" maxlength="20"
                   lay-verType="tips" lay-verify="required" disabled id="parent_name"/>
        </div>
    </div>
    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary layui-btn-sm" type="button" ew-event="closePageDialog">取消</button>
        <button class="layui-btn layui-btn-sm" lay-filter="addCatalogSubmit" lay-submit >确定</button>
    </div>
</form>
<script src="../../../assets/libs/axios.min.js"></script>
<script>
    //获取权限方法
    var userInfo = getAuthority();
    var powerlist = JSON.parse(userInfo).permissions;
    /*自定义函数判断权限*/
    template.defaults.imports.hasPower = function(data){
        if(powerlist.includes(data)){
            return true;
        }
        return true;
    };

    layui.use(['layer', 'form', 'admin','tree'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var tree = layui.tree;

        var Authorization;//请求接口用
        var tableCatalog = window.tableCatalog ;

        if(userInfo){
            var access_token = localStorage.getExpire("access_token");
            var token_type = localStorage.getExpire("token_type");
            Authorization = token_type+" "+access_token;
        }

        if(tableCatalog.hasOwnProperty('selectedCatalog')){
            $("#parent_name").val(tableCatalog.selectedCatalog.name);
        }else{
            $("#parent_name").val('根目录');
        }

        // 表单提交事件
        form.on('submit(addCatalogSubmit)', function (data) {
            let req = {
                name:data.field.name,
                parent_id:tableCatalog.selectedCatalog.id||'root'
            }
            axios({
                method: 'POST',
                url:macroUrl.catalog.addCatalog,
                data:$.param(req),
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.status == 200 &&response.data.message == "success"){
                        layer.msg("新增成功", {icon: 1});
                        window.addIndexMenu = true;
                        admin.closeDialog('#addCatalogForm');  // 关闭页面层弹窗
                    }else{
                        layer.msg(response.data.message, {icon: 2});
                    }
                    layer.closeAll('loading');
                })
                .catch(function (error) {
                    console.log(error);
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
            return false;

        });

    });
</script>